package com.blockchain.securewallet.activities;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blockchain.securewallet.R;
import com.blockchain.securewallet.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

public class SeedQrCodeActivity extends BaseActivity {

    private static final String TAG = "Wallet.SeedQrCodeView";

    @BindView(R.id.iv_qr_code)
    ImageView mQrCode;

    @BindView(R.id.btn_back)
    ImageButton btnBack;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    private String seedWords;

    public static final String KEY_WORDS = "key_words";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seed_qr_code);
        ButterKnife.bind(this);
        tvTitle.setText(R.string.watch_seed);
        Intent intent = getIntent();
        seedWords = intent.getStringExtra(KEY_WORDS);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        createChineseQRCode();
    }

    private void createChineseQRCode() {
        new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                return QRCodeEncoder.syncEncodeQRCode(seedWords, BGAQRCodeUtil.dp2px(SeedQrCodeActivity.this, 150));
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (bitmap != null) {
                    mQrCode.setImageBitmap(bitmap);
                    mQrCode.setVisibility(View.VISIBLE);
                } else {
                    Toast.makeText(SeedQrCodeActivity.this, "生成中文二维码失败", Toast.LENGTH_SHORT).show();
                }
            }
        }.execute();
    }

    @OnClick(R.id.btn_back)
    void back(){
        finish();
    }
}
